package com.ku.kill.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ku.kill.controller.vo.ItemVO;
import com.ku.kill.error.BusinessException;
import com.ku.kill.error.EmBusinessError;
import com.ku.kill.response.CommonReturnType;
import com.ku.kill.service.ItemService;
import com.ku.kill.service.PromoService;
import com.ku.kill.service.model.ItemModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: kill2
 * @description:
 * @author: JokerKu
 * @create: 2020-04-02 22:51
 **/
@RestController
@CrossOrigin(origins = {"*"}, allowCredentials = "true")
@RequestMapping("/item")
@Slf4j
public class ItemController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private PromoService promoService;


    /**
     * 将库存放在redis中后 手动发布活动
     * @param promoId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/publishPromo")
    public CommonReturnType publishPromo(@RequestParam("promoId") Integer promoId) throws BusinessException {
        promoService.publishPromo(promoId);
        return CommonReturnType.create(null);
    }

    @GetMapping("/list")
    public CommonReturnType getList() {
        List<ItemModel> itemModels = itemService.listItem();
        //使用steam API 将model集合转换为VO集合
        List<ItemVO> itemVOList = itemModels.stream().map(itemModel -> {
            return convertVOFromModel(itemModel);
        }).collect(Collectors.toList());
        return CommonReturnType.create(itemVOList);
    }

    @GetMapping("/get")
    public CommonReturnType getItem(@RequestParam("id") Integer id) throws BusinessException {
        if (id == null) {
            throw new BusinessException(EmBusinessError.PARMATER_VALIDATION_ERROR, "请输入商品id");
        }
        ItemModel itemModel = itemService.getItemWithCacheById(id);
        ItemVO itemVO = convertVOFromModel(itemModel);
        return CommonReturnType.create(itemVO);
    }

    //创建商品
    @PostMapping(value = "/create")
    public CommonReturnType createItem(@RequestParam(name = "title") String title,
                                       @RequestParam(name = "description") String description,
                                       @RequestParam(name = "price") BigDecimal price,
                                       @RequestParam(name = "stock") Integer stock,
                                       @RequestParam(name = "imgUrl") String imgUrl) throws BusinessException {
        //封装service请求用来创建商品
        ItemModel itemModel = new ItemModel();
        itemModel.setTitle(title);
        itemModel.setDescription(description);
        itemModel.setPrice(price);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);

        ItemModel itemModelForReturn = itemService.createItem(itemModel);
        ItemVO itemVO = convertVOFromModel(itemModelForReturn);

        return CommonReturnType.create(itemVO);
    }

    //model-->VO
    private ItemVO convertVOFromModel(ItemModel itemModel) {
        ItemVO itemVO = new ItemVO();
        BeanUtil.copyProperties(itemModel, itemVO);
        //判断是否有秒杀活动
        //有秒杀活动
        if (itemModel.getPromoModel() != null) {
            itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVO.setPromoId(itemModel.getPromoModel().getId());
            itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
            itemVO.setStartDate(itemModel.getPromoModel().getStartDate());
        } else {
            itemVO.setPromoStatus(0);
        }
        return itemVO;
    }

}
